// 使用Mock.js生成随机数据
import Mock from 'mockjs'

const Random = Mock.Random

// 生成天气数据，包含温度、湿度、风速、降雨量和预警信息
const generateWeatherData = () => ({
    temperature: Random.float(15, 35, 1, 1),
    humidity: Random.float(30, 90, 1, 1),
    windSpeed: Random.float(0, 30, 1, 1),
    rainfall: Random.float(0, 100, 1, 1),
    warning: Random.boolean() ? Random.pick(['暴雨预警', '雷电预警', '高温预警']) : null
})

// 生成电池组数据，包含多个电池的状态信息
const generateBatteryData = () => {
    const batteries = []
    for (let i = 0; i < 24; i++) {
        batteries.push({
            id: i + 1,
            soc: Random.float(20, 100, 1, 1),
            temp: Random.float(20, 45, 1, 1),
            voltage: Random.float(45, 52, 2, 2),
            current: Random.float(-100, 100, 2, 2),
            status: Random.pick(['normal', 'warning', 'error'])
        })
    }
    return batteries
}

// 生成调度记录
const generateDispatchLogs = () => {
    const logs = []
    for (let i = 0; i < 10; i++) {
        logs.push({
            time: Random.datetime(),
            type: Random.pick(['充电', '放电', '待机']),
            operator: Random.cname(),
            content: Random.sentence(5, 10)
        })
    }
    return logs
}

// 扩展原有的系统数据生成函数
const generateSystemData = () => ({
    inverterPower: Random.float(100, 1000, 2, 2),
    batterySOC: Random.float(20, 100, 2, 2),
    batteryTemp: Random.float(20, 45, 1, 1),
    solarPower: Random.float(0, 500, 2, 2),
    loadPower: Random.float(50, 800, 2, 2),
    gridStatus: Random.boolean(),
    deviceStatus: {
        inverter: Random.pick(['normal', 'warning', 'error']),
        battery: Random.pick(['charging', 'discharging', 'idle']),
        solar: Random.pick(['normal', 'offline']),
        grid: Random.pick(['connected', 'disconnected'])
    },
    alarms: Random.shuffle([{
            type: 'error',
            message: '电池温度过高',
            time: Random.datetime()
        },
        {
            type: 'warning',
            message: '逆变器效率降低',
            time: Random.datetime()
        }
    ]),
    weather: generateWeatherData(),
    batteries: generateBatteryData(),
    dispatchLogs: generateDispatchLogs(),
    charts: {
        power: {
            time: Array.from({ length: 24 }, (_, i) => `${i}:00`),
            values: Array.from({ length: 24 }, () => Random.float(0, 1000, 2, 2))
        },
        alarms: {
            types: ['设备故障', '通信异常', '性能告警', '环境告警'],
            counts: Array.from({ length: 4 }, () => Random.integer(0, 100))
        }
    }
})

// 导出接口配置
export default [
    {
        url: '/api/dashboard/realtime',
        method: 'get',
        response: () => ({
            code: 200,
            data: generateSystemData()
        })
    },
    {
        url: '/api/dashboard/history',
        method: 'get',
        response: ({ query }) => ({
            code: 200,
            data: {
                // 根据请求参数返回不同时间维度的历史数据
                timeRange: query.range,
                data: Array.from({ length: 30 }, () => generateSystemData())
            }
        })
    },
    {
        url: '/api/dashboard/devices',
        method: 'get',
        response: ({ query }) => ({
            code: 200,
            data: {
                // 根据请求参数返回不同时间维度的历史数据
                timeRange: query.range,
                data: Array.from({ length: 30 }, () => generateSystemData())
            }
        })
    },

    {
        url: '/api/heartbeat',  // 心跳接口
        method: 'get',
        response: (config) => {
            // 检查请求头中是否有token
            const token = config.headers?.authorization
            // 如果没有token或token格式不正确，返回401未授权错误
            if (!token || !token.startsWith('Bearer ')) {
                return {
                    code: 401,
                    message: 'Unauthorized: Token is missing or invalid'
                }
            }

            // 模拟偶尔的网络波动，1%的概率返回错误
            if (Math.random() < 0.01) {
                return {
                    code: 500,
                    message: 'Server connection error'
                }
            }

            // 返回正常响应
            // - code: 状态码，200表示正常，前端会显示绿色图标
            // - data: 包含时间戳和状态信息
            return {
                code: 200,  // 可以改为200测试正常状态
                data: {
                    timestamp: Date.now(),  // 当前时间戳
                    status: 'ok'            // 状态信息
                }
            }
        }
    }
]